package LocalDNSServer2;

import LocalDNSServer2.MyClass.Buffer;
import LocalDNSServer2.MyClass.WaitReq;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Author: 0162977
 * @Date: 2020/12/2 14:57
 */
public class ShareSpaceAndFun {
    public static int myPort = 4021 ;
    private static boolean typeToCheck ;  //查询方式
    public static List<Buffer> buf = new ArrayList<Buffer>();
    public static int bufferSize =50;

    private static List<WaitReq> waitReqList = new ArrayList<WaitReq>();

    private static AtomicBoolean notify = new AtomicBoolean(false);
    private static AtomicBoolean notify_buf = new AtomicBoolean(false);

    static {
        typeToCheck = false;

        Buffer b = new Buffer();
        b.setCount(0);
        b.setIp("10.0.0.1");
        b.setWebsite("www.jxufe.edu.cn");
        b.setTimeStamp(System.currentTimeMillis());
        buf.add(b);
    }

    public static boolean getTypeOfCheck(){
        return typeToCheck;
    }

    public static String checkBuf(String website){

        for (int i= 0; i<buf.size() ; i=i+1){
            if (buf.get(i).getWebsite().equals(website)){
                Buffer bufEle = buf.get(i);
                bufEle.setCount(bufEle.getCount()+1);
                buf.set(i,bufEle);
                return bufEle.getIp();
            }
        }
        return null;
    }

    public static boolean addWait(String website, int waitPort){

        while (!notify.compareAndSet(false,true)){;}
        for (int i = 0; i<waitReqList.size(); i++){
            if (waitReqList.get(i).getWebsite().equals(website)){
                if (waitReqList.get(i).getWaitPort().contains(waitPort)){
                    notify.set(false);
                    return false;
                }
                waitReqList.get(i).getWaitPort().add(waitPort);
                System.out.println(waitReqList);
                notify.set(false);
                return false;
            }
        }
        WaitReq waitReq = new WaitReq();
        waitReq.setWebsite(website);
        waitReq.setWaitPort(new ArrayList<Integer>() {{ add(waitPort); } });
        waitReq.setTimeStamp(new Date().getTime());

        waitReqList.add(waitReq);

        System.out.println(waitReqList);
        notify.set(false);
        return true;
    }
    public static List<Integer> getWaitPortByWebsite(String website){

        while (!notify.compareAndSet(false,true)){;}
        for (int i= 0; i< waitReqList.size(); i++){
            if (waitReqList.get(i).getWebsite().equals(website)){
                List<Integer> retList = waitReqList.get(i).getWaitPort();
                waitReqList.remove(i);
                System.out.println(waitReqList);
                notify.set(false);
                return retList;
            }
        }
        notify.set(false);
        return null;
    }

    public static void updateBuf(String website, String Ip){

        while (!notify_buf.compareAndSet(false,true)){;}
        for(Buffer buffer : buf){
            if (buffer.getWebsite().equals(website)){
                buffer.setIp(Ip);
                buffer.setTimeStamp(System.currentTimeMillis());
                buffer.setCount(0);
                notify_buf.set(false);
                return;
            }
        }
        if (buf.size()<bufferSize){
            Buffer b = new Buffer();
            b.setCount(0);
            b.setIp(Ip);
            b.setWebsite(website);
            b.setTimeStamp(System.currentTimeMillis());
            buf.add(b);
        }else{
            int index = 0;
            int minCount = 100;
            Long startTs = System.currentTimeMillis();
            for(int i= 0; i<buf.size()  ;i++){
                if (startTs - buf.get(i).getTimeStamp() >3000 && buf.get(i).getCount()<minCount){
                    index = i;
                }
            }
            buf.remove(index);
            Buffer bb =new Buffer();
            bb.setCount(0);
            bb.setIp(Ip);
            bb.setWebsite(website);
            bb.setTimeStamp(System.currentTimeMillis());
            buf.add(bb);
        }

        notify_buf.set(false);
    }

    public static void main(String[] args) throws InterruptedException {
        updateBuf("111","123.1.1.1");
        System.out.println(buf);
        Thread.sleep(1000);
        updateBuf("111","123.1.1.1");
        System.out.println(buf);
        Thread.sleep(1000);
        updateBuf("222","222.1.1.1");
        System.out.println(buf);
        Thread.sleep(1000);
        updateBuf("333","333.1.1.1");
        System.out.println(buf);
        Thread.sleep(1000);
        updateBuf("444","444.1.1.1");
        System.out.println(buf);
    }

}
